package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Permission;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created by miroshnik on 12/28/2014.
*/
public class PermissionDao extends AbstractDao<Permission> {

    public PermissionDao(Connection connection) throws SQLException {
        super(connection);
    }

    @Override
    public String getSelectQuery() {
        return null;
    }

    @Override
    public String getSelectByIdQuery() {
        return null;
    }

    @Override
    public String getInsertQuery() {
        return null;
    }

    @Override
    public String getUpdateQuery() {
        return null;
    }

    @Override
    public String getDeleteQuery() {
        return null;
    }

    @Override
    protected void prepareInsertStatement(CallableStatement statement, Permission object) throws SQLException {

    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, Permission object) throws SQLException {

    }


    public String getRolePermissionsSelectQuery() {
        return "SELECT PERMISSION.OBJECT_ID PERMISSION_ID, PERM_TITLE.VALUE PERMISSION_TITLE\n" +
                "FROM OBJREFERENCE ROLE_PERM, OBJECTS PERMISSION, ATTRIBUTES PERM_TITLE\n" +
                "WHERE ROLE_PERM.OBJECT_ID = ?\n" +
                "AND ROLE_PERM.REFERENCE = PERMISSION.OBJECT_ID\n" +
                "AND PERM_TITLE.OBJECT_ID = PERMISSION.OBJECT_ID\n" +
                "AND PERM_TITLE.ATTR_ID = 10\n";
    }

    public List<Permission> readByRoleId(int roleId) throws SQLException {
        List<Permission> perms = new ArrayList<>();
        try (PreparedStatement statement = connection.prepareStatement(getRolePermissionsSelectQuery())) {
            statement.setInt(1, roleId);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                perms.add(new Permission(rs.getInt("PERMISSION_ID"), rs.getString("PERMISSION_TITLE")));
            }

            return perms;
        }
    }


    @Override
    protected List<Permission> parseResultSet(ResultSet rs) throws SQLException {
        return null;
    }
}
